<?php
/**
 * 角色管理
 */
class RoleApp extends CommonApp{

	/**
	 * 角色列表页
	 */
	public function indexAct(){
		$name = I('get.name');
		$create_time = I('get.create_time');
		$where = '1 ';
		if($name!='') 
			$where .= "AND `name` LIKE '%{$name}%' ";
		if(My::is_date($create_time))
			$where .= "AND `create_time` like '{$create_time}%' ";
		//page
		$page = My::page(DB('role')->where($where)->count());
		//result
		$roles = DB('role')->where($where)->order("id DESC")->limit($page['limits'])->select();
		//assign
		$this->assign('roles', $roles);
		$this->assign('page', $page);
		$this->display();
	}

	/**
	 * 添加角色
	 */
	public function modifyAct(){
		$id = My::is_id(My::arr2var($_GET,'id'))?$_GET['id']:0;
		$trees = M('category')->item2submenu('menu',array('level'=>3,'hasParent'=>true));//所有菜单列表,3个级数
		$one = array();
		$num = 0;
		if(My::is_id($id)){//编辑
			$one = DB('role')->where("`id`={$id}")->find();
			$menus = ($one['module_ids']!="")?explode(',',$one['module_ids']):'';
			if(is_array($trees))
			foreach($trees as $value){
				$result[$num]['id']  	= $value['id'];
				$result[$num]['pId'] 	= $value['parent_id'];
				$result[$num]['open']	= (isset($value['level']) AND $value['level']==2)?'false':'true';
				$result[$num]['name']	= $value['title'];
				$result[$num]['checked']= (is_array($menus) AND in_array($value['id'],$menus))?'true':'false';
				$num++;
			}
		}else{//添加
			if(is_array($trees))
			foreach($trees as $value){
				$result[$num]['id']=$value['id'];
				$result[$num]['pId']=$value['parent_id'];
				$result[$num]['open']=(isset($value['level']) AND $value['level']==2)?'false':'true';
				$result[$num]['name']=$value['title'];
				$num++;
			}
		}
		//print_r($result);
		$this->assign('one', $one);//当前角色数据
		$this->assign('trees', json_encode($result));
		$this->display();
	}
	
	public function saveAct(){//编辑/修改 保存
		//echo "<pre>";print_r($_POST);exit;
		$id = My::arr2var($_POST,'id');
		if(My::arr2var($_POST,'name')=='') $this->ajaxReturn(300,'角色名称不能为空!');
		if(!My::arr2var($_POST,'module_ids')) $this->ajaxReturn(300,'请选择授权菜单!');
		if(My::is_id($id)){//编辑
			if(DB('role')->clear()->save($_POST)>=0){
				self::updateRoleLimit(My::arr2var($_POST,'module_ids'));//更新当前登录用户的权限session
				$this->ajaxReturn(200, '编辑成功!');
			}else{
				$this->ajaxReturn(300, '编辑失败!');
			}
		}else{//添加
			$_POST['create_time'] = date('Y-m-d H:i:s');
			if(DB('role')->clear()->add($_POST)){
				self::updateRoleLimit(My::arr2var($_POST,'module_ids'));//更新当前登录用户的权限session
				$this->ajaxReturn(200, '添加成功!');
			}else{
				$this->ajaxReturn(300, '添加失败!');
			}
		}
	}
	
	private function updateRoleLimit() {//更新当前登录用户的权限session
		$_SESSION['myMenuCacheId'] = '';//清空我的菜单缓存id
	}
	
	/**
	 * 删除角色
	 */
	public function deleteAct(){
		//print_r($_POST);exit;
		$idArr = My::arr2var($_REQUEST,'check_id');
		if(!$idArr)	$this->ajaxReturn(200, '编号错误');
		if(in_array(1, $idArr)){
			$this->ajaxReturn(200, '无法删除超级管理员角色');
		}
		$str = '';
		if(is_array($idArr))
		foreach($idArr as $value){
			$value = (int)$value;
			if(DB('user')->where("role_id='{$value}'")->count()>0){
				$str .= "删除失败, 尚有用户归属该角色! (ID={$value})<br>";
			}else{
				$ok = DB('role')->where("id='{$value}'")->delete();
				if($ok)
					$str .= "删除成功! (ID={$value})<br>";
				else
					$str .= "删除失败! (ID={$value})<br>";
			}
		}
		$this->ajaxReturn(200, $str);
	}

}